
import * as utc from "../util/utc.js";
import {tr} from "../ux/translations.js";
import {capitalize} from "../util/strings.js";
import * as clock from "../util/clock.js";
import {ø} from "../util/objects.js";

export function describeSurface(attr) {
    return attr.z_level === "surface" ? tr`Surface` : capitalize(attr.z_level);
}

// UNDONE: better more generic name? not really specific to "validTime" calculation
export function resolveValidTime(time_cursor, alignment, offset) {
    const parts = (time_cursor ?? "current") === "current" ? utc.parts(clock.now()) : time_cursor;
    const aligned = utc.floor(parts, alignment);
    return utc.normalize(utc.add(aligned, offset ?? {}));
}

// UNDONE: this function doesn't do much
export function newProduct() {
    return {
        descriptionHTML: undefined,

        paths: [],

        /** @returns {Object} the product's date parts. */
        validTime: function() {
            return null;
        },

        /** @returns {string} the product's appropriate date format string. */
        dateFormat: function() {
            return "{YYYY}-{MM}-{DD} {hh}:{mm} {TZ}";
        },

        alpha: {single: 160, animated: 112},
    };
}

export function nav1hr(time_cursor) {
    const alignment = {hour: 1};
    const validTime = resolveValidTime(time_cursor, alignment);
    const stepper = step => function() {
        return resolveValidTime(utc.add(this.validTime(), step), alignment);
    };
    return {
        alignment: () => ø(alignment),
        validTime: () => ø(validTime),
        prev2: stepper({hour: -8}),
        prev1: stepper({hour: -1}),
        next1: stepper({hour: +1}),
        next2: stepper({hour: +8}),
        navLabels: {
            prev2: `-8 ${tr`hours`}`,
            prev1: `-1 ${tr`hour`}`,
            next1: `+1 ${tr`hour`}`,
            next2: `+8 ${tr`hours`}`,
        },
    };
}

export function nav3hr(time_cursor) {
    const alignment = {hour: 3};
    const validTime = resolveValidTime(time_cursor, alignment);
    const stepper = step => function() {
        return resolveValidTime(utc.add(this.validTime(), step), alignment);
    };
    return {
        alignment: () => ø(alignment),
        validTime: () => ø(validTime),
        prev2: stepper({day:  -1}),
        prev1: stepper({hour: -3}),
        next1: stepper({hour: +3}),
        next2: stepper({day:  +1}),
        navLabels: {
            prev2: `-1 ${tr`day`}`,
            prev1: `-3 ${tr`hours`}`,
            next1: `+3 ${tr`hours`}`,
            next2: `+1 ${tr`day`}`,
        },
    };
}
